package com.wedu.DAO.impl;

import com.wedu.DAO.UserDao;
import com.wedu.entity.Job;
import com.wedu.entity.User;
import com.wedu.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.xml.stream.StreamFilter;
import java.util.List;


public class UserDaoImpl implements UserDao {
    //获取JDBCTemplate对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //判断账号是否已注册过
    @Override
    public User staff(String uid) {
        //定义sql,判断该学号或教师编号是否已注册
        try {
            String sql="select * from `user` where uid = ? or acid=? ";
            return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), uid,uid);
        }catch (Exception e){
            return null;
        }

    }

    @Override
    public User school_staff(String uid) {
        //判断该人员是否为在校人员
        try {
            String sql="select * from the_school_staff where uid=?;";
            User user=template.queryForObject(sql,new BeanPropertyRowMapper<User>(User.class),uid);
            return user;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public User acid_1(String acid) {
        try {
            String sql="select * from `user` where acid = ? ";
            return template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), acid);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public int add_user(User user) {
        try{
            String sql="insert into `user`(uid,`name`,`password`,age,sex,phone,department,date,acid) values(?,?,?,?,?,?,?,?,?)";
            int update = template.update(sql, user.getUid(), user.getName(), user.getPassword(), user.getAge(), user.getSex(), user.getPhone()
                    , user.getDepartment(), user.getDate(), user.getACID());
            return update;
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int update_user(String id, String password) {
        try{
            String sql="update `user` set `password`=? ,`index`=5 where uid=? or acid=?;";
            return template.update(sql, password, id, id);
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public void update_index(int index,String id) {
        String sql="update `user` set `index`=? where uid=? or acid=?;";
        template.update(sql,index,id,id);
    }

    @Override
    public int update_user(User user) {
        try{
            String sql="update `user` set age=?,sex=?,`password`=?,phone=?,department=? where acid=?;";
            return template.update(sql, user.getAge(), user.getSex(), user.getPassword(), user.getPhone(), user.getDepartment(), user.getACID());
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public int update_level(String acid,int level) {
        try {
            String sql="update `user` set credit_rating=? where acid=?";
            return template.update(sql, level, acid);
        }catch (Exception e){
            return 0;
        }
    }

    @Override
    public void remove_user(String acid) {
        String sql="delete from `user` where acid=?;";
        template.update(sql,acid);
    }

    @Override
    public int addPersonnel(String id, String name) {
        String sql="insert into the_school_staff values(?,?);";
        return template.update(sql,id,name);
    }

    @Override
    public List<User> user() {
        String sql="select * from `user`";
        return template.query(sql,new BeanPropertyRowMapper<User>(User.class));
    }

    @Override
    public void add_information(Job job) {
        String sql="insert into part_time_table (job_name,JobType_name,acid,job_level,salary,job_content,demand,job_time,job1_time,deduct_salary,stare,JID,company_name)" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?);";
        template.update(sql,job.getJob_name(),job.getJobType_name(),job.getAcid(),job.getJob_level(),job.getSalary(),job.getJob_content(),job.getDemand()
        ,job.getJob_time(),job.getJob1_time(),job.getDeduct_salary(),job.getStare(),job.getJID(),job.getCompany_name());
    }

    @Override
    public List<Job> job(String acid) {
        try {
            String sql="select * from part_time_table where acid=?";
            return template.query(sql, new BeanPropertyRowMapper<Job>(Job.class),acid);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public Job information(int did,String acid) {
        try {
            String sql="select * from part_time_table where DID=? and acid=?";
            return template.queryForObject(sql,new BeanPropertyRowMapper<Job>(Job.class),did,acid);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<String> acid(int did) {
        String sql="select job_acid from worker_table where DID=? and work_stare=?";
        List<String> query = template.query(sql, new BeanPropertyRowMapper<String>(String.class), did, 1);
        sql="delete from work_table where DID=?";
        template.update(sql,did);
        return query;
    }

    @Override
    public void update_user_balance(String acid,double salary) {
        String sql="update `user` set balance=balance+? where acid=?;";
        template.update(sql,salary,acid);
    }

    @Override
    public void revoke_information(int did) {
        String sql="delete from part_time_table where DID=?";
        template.update(sql,did);
    }

    @Override
    public List<Job> information() {
        String sql="select * from part_time_table where Approval_status=?";
        return template.query(sql,new BeanPropertyRowMapper<Job>(Job.class),1);
    }

    @Override
    public Job information(int did) {
        try {
            String sql="select * from part_time_table where DID=? ";
            return template.queryForObject(sql,new BeanPropertyRowMapper<Job>(Job.class),did);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void handleG(int did, int status) {
        String sql="update part_time_table set Approval_status=? where DID=?";
        template.update(sql,status,did);
    }

    @Override
    public List<Job> looks_over() {
        try {
            String sql="select * from part_time_table where Approval_status=? ";
            return template.query(sql,new BeanPropertyRowMapper<Job>(Job.class),2);
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void update_job_level(int did,int level) {
        String sql="update  part_time_table set job_level=? where DID=?";
        template.update(sql,level,did);
    }

    @Override
    public void update_jobType_name(int did, String jobType_name) {
        String sql="update  part_time_table set JobType_name=? where DID=?";
        template.update(sql,jobType_name,did);
    }

    @Override
    public void update_deduct_salary(int did, double deduct_salary) {
        String sql="update part_time_table set deduct_salary=? where DID=?";
        template.update(sql,deduct_salary,did);
    }

    @Override
    public List<Job> completed() {
        try {
            String sql="select * from part_time_table where stare='false' and demand=0";
            return template.query(sql, new BeanPropertyRowMapper<Job>(Job.class));
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<Job> worker() {
        try {
            String sql="select* from worker_table";
            return template.query(sql,new BeanPropertyRowMapper<Job>(Job.class));
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public void addWorker(Job job) {
        String sql="insert into worker_table (DID,job_name,JobType_name,acid,job_level,salary,job_time,job1_time,deduct_salary,job_content,JID,company_name,Uid,job_acid)" +
                "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        template.update(sql,job.getDID(),job.getJob_name(),job.getJobType_name(),job.getAcid(),job.getJob_level(),job.getSalary(),job.getJob_time(),job.getJob1_time(),
                job.getDeduct_salary(),job.getJob_content(),job.getJID(),job.getCompany_name(),job.getUid(),job.getJob_acid());
        sql="update part_time_table set stare=?,demand=? where DID=?";
        template.update(sql,job.getStare(),job.getDemand(),job.getDID());
    }

    @Override
    public void update_work_stare(int did) {
        String sql="update worker_table set work_stare=? where DID=?";
        template.update(sql,0,did);
    }

    @Override
    public void update_stare(int did) {
        String sql="update part_time_table set stare=? where DID=?";
        template.update(sql,"false",did);
    }
}
